<template>
	<view>
		<z-paging ref="paging" v-model="dataList" @query="queryList">
			<template #top>
				<view class="status_bar"></view>
			</template>
			
			<view class="item" v-for="(item, index) in dataList" :key="index">
				<view class="top">
					<image :src="item.headPicUrl"></image>
					<view class="nick">{{item.nick}}</view>
					<image :src="getImageUrl('ic_sex_girl.png')" style="height: 25rpx;" mode="heightFix" v-if="2==item.sex"></image>
					<image :src="getImageUrl('ic_sex_boy.png')" style="height: 25rpx;" mode="heightFix" v-if="1==item.sex"></image>
					<view style="flex:1;"></view>
					<view class="time">{{item.createTime}}</view>
					<image class="delete" :src="getImageUrl('ic_delete.png')" @click="onDelete(item, index)" v-if="item.ownFlag"></image>
				</view>
				<view class="content">{{item.content}}</view>
				<view class="bottom">
					<image v-for="(url,index) in item.fileUrl" :src="url" @click="onPreviewImage(item.fileUrl, index)"></image>
				</view>
			</view>
			
			<template #bottom>
				<image :src="getImageUrl('ic_release.png')" class="release" @click="toRelease"></image>
			</template>
		</z-paging>
		
	</view>

</template>

<script>
	export default {
		data() {
			return {
				dataList: [],
			};
		},
		onLoad() {
			uni.$on('sendDynamicSuccess', ()=>{
				this.queryList(1, 20)
			})
		},
		onUnload() {
			uni.$off('sendDynamicSuccess')
		}, 
		methods: {
			async queryList(pageNo, pageSize) {
				const params = {
					pageNo: pageNo,
					type:'1'
				}
				let res = await this.$common.get("/api/dynamic/getDynamicList", params);
				if (200 == res.code) {
					this.$refs.paging.complete(res.data);
				} else {
					this.$refs.paging.complete(false);
				}
			},
			
			onPreviewImage(list, index){
				console.log("list: " + JSON.stringify(list));
				console.log("index: " + JSON.stringify(index));
				uni.previewImage({
					urls: list,
					current: index,
					fail: (error) => {
						console.log("error: " + JSON.stringify(error));
					}
				})
			},
			toRelease(){
				uni.navigateTo({
					url:'/main/dynamic/release/release'
				})
			},
			onDelete(item, index){
				uni.showModal({
					content:'确定要删除吗？',
					success:(res)=>{
						if(res.confirm){
							console.log("onDelete")
							this.requestDelete(item, index);
						}
					}
				})
			},
			async requestDelete(item, index){
				console.log("item: " + JSON.stringify(item));
				console.log("index: " + JSON.stringify(index));
				let res = await this.$common.get('/api/dynamic/delOwnDynamic', {dynamicId: item.id})
				if(200 == res.code){
					uni.showToast({
						title: '删除成功',
						icon: 'none'
					})
					this.dataList.splice(index, 1)
				}else{
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
	}
	
	.release{
		position: absolute;
		bottom: 30rpx;
		right: 30rpx;
		width: 100rpx;
		height: 100rpx;
		z-index: 99;
	}

	.item {
		display: flex;
		flex-direction: column;
		width: 100wh;
		box-sizing: border-box;
		background-color: white;
		border-radius: 10rpx;
		margin: 20rpx;
		padding: 20rpx;
		
		.top{
			display: flex;
			flex-direction: row;
			align-items: center;
			image{
				width: 60rpx;
				height: 60rpx;
				border-radius: 50%;
			}
			.nick{
				font-size: 28rpx;
				color: #222;
				margin-left: 15rpx;
			}
			.time{
				font-size: 20rpx;
				color: #888;
			}
			.delete{
				width: 30rpx;
				height: 30rpx;
				margin-left: 15rpx;
			}
		}
		
		.content{
			margin: 20rpx 0;
			font-size: 24rpx;
			color: #555;
		}
		
		.bottom{
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;
			width: 670rpx;
			image{
				width: 219rpx;
				height: 219rpx;
				border-radius: 15rpx;
				margin: 2rpx;
			}
		}
	}
</style>